package com.easemob.easeui.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TextView.BufferType;
import android.widget.Toast;

import com.cloudcc.mobile.R;
import com.cloudcc.mobile.entity.CoworkerEntity;
import com.cloudcc.mobile.im.ui.GroupsActivity;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMChatRoom;
import com.easemob.chat.EMConversation;
import com.easemob.chat.EMConversation.EMConversationType;
import com.easemob.chat.EMGroup;
import com.easemob.chat.EMGroupManager;
import com.easemob.chat.EMMessage;
import com.easemob.easeui.utils.EaseCommonUtils;
import com.easemob.easeui.utils.EaseSmileUtils;
import com.easemob.easeui.utils.EaseUserUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 会话列表adapter
 * 
 */
public class EaseConversationAdapater extends ArrayAdapter<EMConversation> {
	private static final String TAG = "ChatAllHistoryAdapter";
	private List<EMConversation> conversationList;
	private List<EMConversation> copyConversationList;
	private ConversationFilter conversationFilter;
	private boolean notiyfyByFilter;
	private String mEns = RunTimeManager.getInstance().getlanguage();

	protected int primaryColor;
	protected int secondaryColor;
	protected int timeColor;
	protected int primarySize;
	protected int secondarySize;
	protected float timeSize;
	protected Context mContext;
	public EaseConversationAdapater(Context context, int resource,
			List<EMConversation> objects) {
		super(context, resource, objects);
		conversationList = objects;
		mContext = context ;
		copyConversationList = new ArrayList<EMConversation>();
		copyConversationList.addAll(objects);
	}

	@Override
	public int getCount() {
		return conversationList.size()+1;
	}

	@Override
	public EMConversation getItem(int arg0) {
		if (arg0 < conversationList.size()) {
			return conversationList.get(arg0);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		if(getItemViewType(position) == 0) {
			if (convertView == null) {
				convertView = LayoutInflater.from(getContext()).inflate(
						R.layout.group_chat_yx, parent, false);
				LinearLayout ll_qunliao = (LinearLayout)convertView.findViewById(R.id.ll_qunliao);
				ll_qunliao.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						if("cc201307019DXlPd701".equals(RunTimeManager.getInstance().getOrgId())){
							Toast.makeText(mContext,mContext.getResources().getString(R.string.tiyan),Toast.LENGTH_SHORT).show();
						}else {
							mContext.startActivity(new Intent(mContext, GroupsActivity.class));
						}
					}
				});
				convertView.setOnTouchListener(new View.OnTouchListener() {
					@Override
					public boolean onTouch(View v, MotionEvent event) {

						return true;
					}
				});


			}

		}else {
			if (convertView == null) {
				convertView = LayoutInflater.from(getContext()).inflate(
						R.layout.ease_row_chat_history, parent, false);
			}
			ViewHolder holder = (ViewHolder) convertView.getTag();
			if (holder == null) {
				holder = new ViewHolder();
				holder.name = (TextView) convertView.findViewById(R.id.name);
				holder.unreadLabel = (TextView) convertView
						.findViewById(R.id.unread_msg_number);
				holder.message = (TextView) convertView.findViewById(R.id.message);
				holder.time = (TextView) convertView.findViewById(R.id.time);
				holder.avatar = (ImageView) convertView.findViewById(R.id.avatar);
				holder.msgState = convertView.findViewById(R.id.msg_state);
				holder.list_itease_layout = (RelativeLayout) convertView
						.findViewById(R.id.list_itease_layout);
				convertView.setTag(holder);
			}
			holder.list_itease_layout
					.setBackgroundResource(R.drawable.ease_mm_listitem);

			// 获取与此用户/群组的会话
			EMConversation conversation = getItem(position - 1);
			// 获取用户username或者群组groupid
			String username = conversation.getUserName();

			if (conversation.getType() == EMConversationType.GroupChat) {
				// 群聊消息，显示群聊头像
				holder.avatar.setImageResource(R.drawable.ease_group_icon);
				EMGroup group = EMGroupManager.getInstance().getGroup(username);
				holder.name
						.setText(group != null ? group.getGroupName() : username);
			} else if (conversation.getType() == EMConversationType.ChatRoom) {
				holder.avatar.setImageResource(R.drawable.ease_group_icon);
				EMChatRoom room = EMChatManager.getInstance().getChatRoom(username);
				holder.name.setText(room != null
						&& !TextUtils.isEmpty(room.getName()) ? room.getName()
						: username);
			} else {// 单聊
				EaseUserUtils.setUserAvatar(getContext(), username, holder.avatar);
				EaseUserUtils.setUserNick(username, holder.name);
			}
			if (conversation.getUnreadMsgCount() > 0) {
				// 显示与此用户的消息未读数
				holder.unreadLabel.setText(String.valueOf(conversation
						.getUnreadMsgCount()));
				if(conversation.getUnreadMsgCount()>99) {
					holder.unreadLabel.setText("99+");
				}
				holder.unreadLabel.setVisibility(View.VISIBLE);
			} else {
				holder.unreadLabel.setVisibility(View.INVISIBLE);
			}

			if (conversation.getMsgCount() != 0) {
				// 把最后一条消息的内容作为item的message内容
				EMMessage lastMessage = conversation.getLastMessage();
				holder.message
						.setText(EaseSmileUtils.getSmiledText(
								getContext(),
								EaseCommonUtils.getMessageDigest(lastMessage,
										(this.getContext()))), BufferType.SPANNABLE);
				SimpleDateFormat sdformat = new SimpleDateFormat("MM-dd HH:mm");//24小时制
				holder.time.setText(sdformat.format(new Date(
						lastMessage.getMsgTime())));

				if (lastMessage.direct == EMMessage.Direct.SEND
						&& lastMessage.status == EMMessage.Status.FAIL) {
					holder.msgState.setVisibility(View.VISIBLE);
				} else {
					holder.msgState.setVisibility(View.GONE);
				}
			}

			// 设置自定义属
			holder.name.setTextColor(primaryColor);
			holder.message.setTextColor(secondaryColor);
			holder.time.setTextColor(timeColor);
			if (primarySize != 0)
				holder.name.setTextSize(TypedValue.COMPLEX_UNIT_PX, primarySize);
			if (secondarySize != 0)
				holder.message.setTextSize(TypedValue.COMPLEX_UNIT_PX,
						secondarySize);
			if (timeSize != 0)
				holder.time.setTextSize(TypedValue.COMPLEX_UNIT_PX, timeSize);
		}

		return convertView;
	}

	@Override
	public int getItemViewType(int position) {
		if(position == 0) {
		    return 0;
		}else {
			return 1;
		}
	}

	@Override
	public int getViewTypeCount() {
		return 2;
	}

	@Override
	public void notifyDataSetChanged() {
		super.notifyDataSetChanged();
		if (!notiyfyByFilter) {
			copyConversationList.clear();
			copyConversationList.addAll(conversationList);
			notiyfyByFilter = false;
		}
	}

	@Override
	public Filter getFilter() {
//		if (conversationFilter == null) {
			conversationFilter = new ConversationFilter(copyConversationList);
//		}
		return conversationFilter;
	}

	public void setPrimaryColor(int primaryColor) {
		this.primaryColor = primaryColor;
	}

	public void setSecondaryColor(int secondaryColor) {
		this.secondaryColor = secondaryColor;
	}

	public void setTimeColor(int timeColor) {
		this.timeColor = timeColor;
	}

	public void setPrimarySize(int primarySize) {
		this.primarySize = primarySize;
	}

	public void setSecondarySize(int secondarySize) {
		this.secondarySize = secondarySize;
	}

	public void setTimeSize(float timeSize) {
		this.timeSize = timeSize;
	}

	private class ConversationFilter extends Filter {
		List<EMConversation> mOriginalValues = null;

		public ConversationFilter(List<EMConversation> mList) {
			mOriginalValues = mList;
		}

		@Override
		protected FilterResults performFiltering(CharSequence prefix) {
			FilterResults results = new FilterResults();

			if (mOriginalValues == null) {
				mOriginalValues = new ArrayList<EMConversation>();
			}
			if (prefix == null || prefix.length() == 0) {
				results.values = copyConversationList;
				results.count = copyConversationList.size();
			} else {
				String prefixString = prefix.toString();
				final int count = mOriginalValues.size();
				final ArrayList<EMConversation> newValues = new ArrayList<EMConversation>();

				for (int i = 0; i < count; i++) {
					final EMConversation value = mOriginalValues.get(i);
					String username = value.getUserName();

					EMGroup group = EMGroupManager.getInstance().getGroup(
							username);
					if (group != null) {
						username = group.getGroupName();
					} else {
						CoworkerEntity user = EaseUserUtils
								.getUserInfo(username);
						if (user != null && user.getNick() != null)
							username = user.getNick();
					}

					// First match against the whole ,non-splitted value
					if (username.contains(prefixString)) {
						newValues.add(value);
					} else {
						final String[] words = username.split(" ");
						final int wordCount = words.length;

						// Start at index 0, in case valueText starts with
						// space(s)
						for (int k = 0; k < wordCount; k++) {
							if (words[k].contains(prefixString)) {
								newValues.add(value);
								break;
							}
						}
					}
				}

				results.values = newValues;
				results.count = newValues.size();
			}
			return results;
		}

		@Override
		protected void publishResults(CharSequence constraint,
				FilterResults results) {
			conversationList.clear();
			try {
				conversationList.addAll((List<EMConversation>) results.values);
			} catch (Exception e) {
				e.printStackTrace();
			}
			if (results.count > 0) {
				notiyfyByFilter = true;
				notifyDataSetChanged();
			} else {
				notifyDataSetInvalidated();
			}

		}

	}

	private static class ViewHolder {
		/** 和谁的聊天记录 */
		TextView name;
		/** 消息未读数 */
		TextView unreadLabel;
		/** 最后一条消息的内容 */
		TextView message;
		/** 最后一条消息的时间 */
		TextView time;
		/** 用户头像 */
		ImageView avatar;
		/** 最后一条消息的发送状态 */
		View msgState;
		/** 整个list中每一行总布局 */
		RelativeLayout list_itease_layout;

	}
}
